package com.lantosec.hmrp.modules.sysdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.BeanUtils;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataForm;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataFormExt;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataModel;
import com.lantosec.hmrp.modules.sysdata.mapper.SysDataFormExtMapper;
import com.lantosec.hmrp.modules.sysdata.mapper.SysDataFormMapper;
import com.lantosec.hmrp.modules.sysdata.service.ISysDataFormService;
import com.lantosec.hmrp.modules.sysdata.service.ISysDataModelService;
import com.lantosec.hmrp.modules.sysdata.vo.FieldVO;
import com.lantosec.hmrp.modules.sysdata.vo.SysDataFormVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统数据表单定义表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-02
 */
@Service
@Primary
@Log4j2
public class SysDataFormServiceImpl extends ServiceImpl<SysDataFormMapper, SysDataForm> implements ISysDataFormService {
    @Autowired
    private SysDataFormExtMapper extMapper;
    @Autowired
    private ISysDataModelService modelService;

    @Override
    public SysDataFormVO get(String id) {
        SysDataForm entity = super.getById(id);
        SysDataModel model = modelService.getById(entity.getDataModelId());
        List<FieldVO> fields = modelService.selectTableFields(model.getDataModelId());
        extMapper.selectList(new QueryWrapper<SysDataFormExt>().eq(SysDataFormExt.ColName.data_form_id.toString(), entity.getDataFormId())).forEach(field -> {
            fields.add(new FieldVO(field.getFieldName(), field.getFieldType(), field.getComment(), false, false, true));
        });
        SysDataFormVO vo = entity.convertToVO();
        vo.setFields(fields);
        return vo;
    }

    @Override
    public SysDataFormVO create(SysDataFormVO vo) {
        SysDataForm entity = vo.convertToEntity();
        SysDataModel model = modelService.getById(vo.getDataModelId());
        List<FieldVO> fields = null;
        if (StringUtils.isNotEmpty(vo.getParentId())) {
            SysDataFormVO parent = get(vo.getParentId());
            entity.setDataFormLayout(parent.getDataFormLayout());
            vo.setDataFormLayout(parent.getDataFormLayout());
            fields = parent.getFields();
        } else {
            fields = modelService.selectTableFields(model.getDataModelId());
        }
        entity.setCreateTime(new Date());
        entity.setEditTime(new Date());
        save(entity);
        vo.setDataFormId(entity.getDataFormId());
        vo.setFields(fields);
        return vo;
    }

    @Override
    public SysDataFormVO getByModelId(String id) {
        return BeanUtils.entity2VO(getOne(new QueryWrapper<SysDataForm>().eq(SysDataForm.ColName.data_model_id.toString(), id), false));
    }
}
